package com.woniuxy.creditsystem.mortgagemag.controller;



import com.github.pagehelper.PageInfo;
import com.woniuxy.creditsystem.common.bean.*;

import com.woniuxy.creditsystem.common.bean.ResultMsg;
import com.woniuxy.creditsystem.mortgagemag.service.IVehicleService;
import com.woniuxy.creditsystem.mortgagemag.util.UploadImg;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;

@RequestMapping("/vehicle")
@RestController
public class VehicleController {

    @Resource
    private IVehicleService vehicleServiceImpl;



    @RequestMapping(value="/page",produces = {"application/json;charset=utf-8"})
    public TableResult<VehicleInfo> findAllByParams(Integer pageNum, Integer pageSize,VehicleFind vehicleFind){
        System.out.println(vehicleFind.getName());
        PageInfo<VehicleInfo> pageInfo = vehicleServiceImpl.findAllByParams( pageNum,pageSize,vehicleFind);
        return new TableResult<VehicleInfo>(pageInfo.getTotal(),pageInfo.getList());
    }

    @PutMapping(value="/{id}",produces = {"application/json;charset=utf-8"})
    public ResultMsg updateVehicle(VehicleInfo vehicleInfo, @PathVariable("id") Long id){
        System.out.println(id);
        System.out.println(vehicleInfo);
        ResultMsg resultMsg = new ResultMsg(200,"新增成功",null);
        vehicleServiceImpl.updateVehicle(vehicleInfo);
        return resultMsg;
    }



    @RequestMapping(value="/more",produces = {"application/json;charset=utf-8"})
    public ResultMsg saveVehicle(VehicleInfo vehicleInfo){

        System.out.println("添加");
        ResultMsg resultMsg = new ResultMsg(200,"新增成功",null);
        CustomerInfo customerInfo =new CustomerInfo();
        customerInfo.setId(1);
        vehicleInfo.setCustomerInfo(customerInfo);
        vehicleServiceImpl.saveVehicleInfo(vehicleInfo);
        return resultMsg;
    }


    @RequestMapping(value = "/importImg",produces = {"application/json;charset=utf-8"})
    public ResultMsg importData(@RequestParam("file") MultipartFile file) throws Exception{
        System.out.println("上传");
        ResultMsg resultMsg = new ResultMsg(200,"上传成功",null);
        String filePath = UploadImg.getImgNameTwo(file);
        resultMsg.setData(filePath);
        return resultMsg;
    }

}
